package com.sanyang.pda.ui.manualsampling;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sanyang.pda.adapter.UnderDetectionAdapter;
import com.sanyang.pda.bean.UnderDetectionBean;
import com.sanyang.pda.contract.UnderDetectionContract;
import com.sanyang.pda.event.SamplingEvent;
import com.sanyang.pda.presenter.UnderDetectionPresenter;
import com.sanyang.pda.utils.StringConfig;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpFragment;
import com.wangshen.projectbase.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class UnderDetectionFragment extends BaseMvpFragment<UnderDetectionPresenter> implements UnderDetectionContract.View  , OnRefreshListener {

    @BindView(R.id.rv_list)
    RecyclerView rvList;
    UnderDetectionAdapter adapter;
   /* @BindView(R.id.tv_hege)
    TextView tvHege;
    @BindView(R.id.tv_no_hege)
    TextView tvNoHege;
    @BindView(R.id.cb_all)
    CheckBox cbAll;*/
    private String en;
    //UnderDetectionDialog underDetectionDialog;
    @BindView(R.id.smart)
    SmartRefreshLayout smart;
    private String qeNo;
    public UnderDetectionFragment() {
    }

    public UnderDetectionFragment(String en,String qeNo) {
        this.en = en;
        this.qeNo=qeNo;
    }
    @Subscribe
    public void onEvent(SamplingEvent samplingEvent){
        smart.autoRefresh();
    }
    @Override
    public UnderDetectionPresenter getPresenter() {
        return new UnderDetectionPresenter();
    }

    @Override
    public int getViewId() {
        return R.layout.fragment_list;
    }

    @Override
    public void initView() {
        smart.setEnableRefresh(true);
        smart.setEnableLoadMore(false);
        smart.setOnRefreshListener(this);
        EventBus.getDefault().register(this);
        adapter = new UnderDetectionAdapter(R.layout.item_under_detection, new ArrayList<>());
        rvList.setLayoutManager(new LinearLayoutManager(mContext));
        rvList.setAdapter(adapter);

    }
    @Override
    public void initLinsenterner() {
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter1, @NonNull View view, int position) {
                Bundle bundle=new Bundle();
                bundle.putString(StringConfig.CODE,adapter.getData().get(position).getSamplingNo());
                startIntent(AppActivityKey.QUALITYDETAILACTIVITY,bundle);
            }
        });
    }

    @Override
    public void getData() {
        smart.autoRefresh();
    }

    @Override
    public void showMessage(String msg) {
        super.showMessage(msg);
        smart.finishRefresh();
    }
    @Override
    public void setList(List<UnderDetectionBean> list) {
        smart.finishRefresh();
        adapter.setNewInstance(list);
    }

    @Override
    public void samplingSuccess() {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        presenter.waitDetectList(qeNo);
    }
}
